//package com.fy.service.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.fy.utils.result.Result;
//import com.fy.mapper.CouponMapper;
//import com.fy.pojo.entity.Coupon;
//import com.fy.pojo.entity.CouponOrder;
//import com.fy.service.CouponService;
//import org.redisson.api.RedissonClient;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.script.DefaultRedisScript;
//import org.springframework.stereotype.Service;
//
//import java.util.Collections;
//import java.util.Date;
//
//@Service
//public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
//    @Autowired
//    private CouponMapper couponMapper;
//
//    @Autowired
//    private RedissonClient redissonClient;
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    private static final DefaultRedisScript<Long> SECKILL_SCRIPT;
//    static {SECKILL_SCRIPT=new DefaultRedisScript<>(); SECKILL_SCRIPT.setLocation(new ClassPathResource("seckill.lua")); SECKILL_SCRIPT.setResultType(Long.class);}
//
//    @Override
//    public Result seckill(Integer userId, Integer couponType) {
//        Long result = (Long) redisTemplate.execute(
//                SECKILL_SCRIPT,
//                Collections.emptyList(),
//                userId, couponType
//        );
////        int stock=0;
////        Object value = redisTemplate.opsForValue().get(Constants.COUPON_STOCK_KEY + coupon.getId());
////        String strValue = value.toString();
////        int intValue = Integer.parseInt(strValue);
////        stock= intValue;
////        if(new Date().before(coupon.getGrabStartTime())){
////            return Result.error("活动还未开始!");
////        }
////
////        if(new Date().after(coupon.getGrabEndTime())){
////            return Result.success("活动已经结束了!");
////        }
////
////        if(stock < 1){
////            return Result.success("优惠券被抢光了!");
////        }
////
////        redisTemplate.opsForSet().add(Constants.REDISSION_LOCK_KEY +coupon.getId(),userId);
////        RLock lock = redissonClient.getLock(Constants.REDISSION_LOCK_KEY +coupon.getId()+ userId);
////
////        boolean tryLock = lock.tryLock();
////
////        if(tryLock && lock.getHoldCount() > 1){
////            return Result.error("禁止重复下单!");
////        }
////
////        redisTemplate.opsForValue().decrement(Constants.COUPON_STOCK_KEY + coupon.getCouponType());
//        int r = result.intValue();
//        if(r==3){
//            return Result.error("优惠券不存在!");
//        }
//
//        if (r != 0) {
//            return Result.error(r == 1 ? "库存不足!" : "不能重复下单!");
//        }
//
//        LambdaQueryWrapper<Coupon> wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(Coupon::getCouponType,couponType);
//        Coupon coupon = couponMapper.selectOne(wrapper);
//
//        CouponOrder couponOrder=new CouponOrder();
//        couponOrder.setUserId(userId);
//        couponOrder.setCouponDesc(coupon.getCouponDescription());
//        couponOrder.setCouponId(coupon.getId());
//        couponOrder.setCreateTime(new Date());
//        couponOrder.setExpireTime(coupon.getCouponExpireTime());
//
//        rabbitTemplate.convertAndSend("seckill","seckill", couponOrder);
//
//        return Result.success("抢券成功!");
//    }
//
//}
